package com.xxl.job.executor.core.utils;


import java.io.*;

public class BackupFiles {

    public static void backFile(String path, File f) {
        String fileName = f.getName();
        InputStream is = null;
        OutputStream os = null;
        int bytesum = 0;
        int byteread = 0;
        try {
            is = new FileInputStream(f);
            // 将数据写入备份文件夹中
            if (ScanFiles.isExist(path)){
                os = new FileOutputStream(path + f.getName());
                byte[] buffer = new byte[1024];
                while ((byteread = is.read(buffer)) != -1){
                    bytesum += byteread;
                    os.write(buffer, 0, byteread);
                }
                os.flush();
            }
        } catch (FileNotFoundException e) {
            System.out.println("目录--" + path + "下的文件--" + fileName + "不存在！");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("备份目录--" + path + "下的文件--" + fileName + "发生错误！");
            e.printStackTrace();
        }finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
